﻿using System;
using Hjt212Parser;
using System.Linq;
using System.Text;
using Biz.Buffer;
using Biz.EF.Platform;
using log4net;


namespace Biz.Process
{
    /// <summary>
    /// 主要处理报警信息
    /// </summary>
    public class UplineAlarmInstruction
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(UplineAlarmInstruction));

        public static void Process2072(PlatformContext pCtx, string content)
        {
            //TODO:2072
        }

        public static void Process2011FlagD(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport = CommonInformation.GetLetportByMn(mn);
                    if (letport == null) return;
                    var hasException = false;
                    var codes = Parser.GetPollutantCodes(content);
                    foreach (var code in codes)
                    {
                        var flag = Parser.GetStringByKey(code + "-Flag", content);
                        if (!string.IsNullOrEmpty(flag) && flag.Equals("D"))
                        {
                            hasException = true;
                            break;
                        }

                    }
                    if (!hasException) return;

                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, 0, 0, 0);
                    var end = begin.AddDays(1);
                    var exception = pCtx.异常情况
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .FirstOrDefault(t => t.时间 >= begin && t.时间 < end);
                    if (exception == null)
                    {
                        exception = new 异常情况();
                        pCtx.异常情况.Add(exception);
                    }
                    var msg = "";
                    foreach (var code in codes)
                    {
                        var flag = Parser.GetStringByKey(code + "-Flag", content);
                        if (!string.IsNullOrEmpty(flag) && !flag.Equals("D")) continue;
                        var factor = CommonInformation.GetContamCode(code);
                        if (factor == null) continue;
                        if (code.Equals("w00000"))
                        {
                            msg += "流量计、";
                        }
                        else
                        {
                            msg += factor.名称 + "、";
                        }
                    }
                    if (msg.Length > 0)
                    {
                        msg = msg.Substring(0, msg.Length - 1);
                    }

                    exception.企业基本信息ID = letport.企业基本信息ID;
                    if (exception.异常情况1 == null || !exception.异常情况1.Contains(msg))
                        exception.异常情况1 = msg + "仪器仪表故障";
                    exception.时间 = datatime.Value;

                    Log.Info(exception.异常情况1);
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

    }
}
